﻿#define _CRT_SECURE_NO_WARNINGS 1


#include <stack>
#include <string>
using namespace std;

class Solution {
public:
    int calculate(string s)
    {
        stack<int> op;
        op.push(1);
        int i = 0;
        int n = s.size();
        int curSign = 1;
        int ret = 0;
        while (i < n)
        {
            if (s[i] == ' ')
                i++;
            else if (s[i] == '+')
            {
                curSign = op.top();
                i++;
            }
            else if (s[i] == '-')
            {
                curSign = -op.top();
                i++;
            }
            else if (s[i] == '(')
            {
                op.push(curSign);   //遇到左括号，将括号前的符入入栈
                i++;
            }
            else if (s[i] == ')')
            {
                op.pop();       //遇到右括号，则(前的符号用完了，出栈
                i++;
            }
            else
            {
                long long num = 0;
                while (i < n && isdigit(s[i]))
                {
                    num = num * 10 + s[i] - '0';
                    i++;
                }
                ret += curSign * num;
            }
        }
        return ret;
    }
};

// 简单⼯⼚模式：通过参数控制可以⽣产任何产品
// 优点：简单粗暴，直观易懂。使⽤⼀个⼯⼚⽣产同⼀等级结构下的任意产品
// 缺点：
// 1. 所有东西⽣产在⼀起，产品太多会导致代码量庞⼤
// 2. 开闭原则遵循(开放拓展，关闭修改)的不是太好，要新增产品就必须修改⼯⼚⽅法。
#include <iostream>
#include <string>
#include <memory>
class Fruit {
public:
    Fruit() {}
    virtual void show() = 0;
};
class Apple : public Fruit {
public:
    Apple() {}
    virtual void show() override {
        std::cout << "我是⼀个苹果" << std::endl;
    }
};
class Banana : public Fruit {
public:
    Banana() {}
    virtual void show()override {
        std::cout << "我是⼀个⾹蕉" << std::endl;
    }
};

class FruitFactory {
public:
    virtual std::shared_ptr<Fruit> create() = 0;
};
class AppleFactory : public FruitFactory {
public:
    virtual std::shared_ptr<Fruit> create() override {
        return std::make_shared<Apple>();
    }
};
class BananaFactory : public FruitFactory {
public:
    virtual std::shared_ptr<Fruit> create() override {
        return std::make_shared<Banana>();
    }
};

int main()
{
    std::shared_ptr<Fruit> fruit = nullptr;
    std::shared_ptr<FruitFactory> factory(new AppleFactory());
    fruit = factory->create();
    fruit->show();

    factory.reset(new BananaFactory());
    fruit = factory->create();
    fruit->show();
    return 0;
}